﻿/// <reference path="jquery-1.2.6.min.js" />

$(document).ready(function() {
    $.timer(7000, function(timer) {// 7 seconds.
        chatProg.keepListAlive(KeepListAlive); //keeps the list alive.
        chatProg.getUsersOnline(updateOnlineUsers); //gets all the users online
    });
    $.timer(10000, function(timer) { //10 seconds.
        updateList(); //updates the buddy list
        chatProg.removeOldGroups(); //removes old groups and adds new ones
    });
    $.timer(30000, function(timer) { //30 seconds.
        chatProg.getUsersOffline(updateOfflineUsers); //gets all offline users.
    });
});

///<summary>
/// updates the online users of the list.
///</summary>
///<param name="results" optional="false">the users in a string[] value</param>
function updateOnlineUsers(results) {
    var Parent = document.getElementById("ulOnline");
    Parent.innerHTML = '';
    for (var i = 0; i < results.length; i++) {
        addLI("ulOnline", results[i], "on");
    }
};

///<summary>
/// updates the offline uses of the list.
///</summary>
///<param name="results" optional="false">the users in a string[] value</param>
function updateOfflineUsers(results) {
    var Parent = document.getElementById("ulOffline");
    Parent.innerHTML = '';
    for (var i = 0; i < results.length; i++) {
        addLI("ulOffline", results[i], "off");
    }
};

///<summary>
/// adds a new user to the selected list.
///</summary>
///<param name="id" optional="false">id of the list.</param>
///<param name="name" optional="false">user name to add</param>
///<param name="onOff" optional="false">if this is and online or offline user.</param>
function addLI(id, name, onOff) {
    var Parent = document.getElementById(id);
    var NewLI = document.createElement("LI");
    $(NewLI).addClass("liBuddyListUsers");
    if (onOff == "on") {
        NewLI.innerHTML = '<img src="../images/IMon.png" /> <a href=\'javascript:commonPopup("chat.aspx?r=' + name + '",400,350,4,"Chat' + name + '");\'>' + name + '</a>';
    }
    else {
        NewLI.innerHTML = '<img src="../images/IMoff.png" /> <a href=\'javascript:commonPopup("chat.aspx?r=' + name + '",400,350,4,"Chat' + name + '");\'>' + name + '</a>';
    }
    Parent.appendChild(NewLI);
};
///<summary>
/// keeps the list alive.
///</summary>
///<param name="results" optional="false">the items to make sure the list stays alive.</param>
function KeepListAlive(results) {
    if (results[0] != '0') {
        if (results[0] == '2' & results[2] != null) {
            chatProg.keepListAlive(KeepListAlive);
        }
        if (results[1] != null & results[1] != '') {
            commonPopup(results[2] + '/IM/chat.aspx?r=' + results[1], 400, 350, 4, 'Chat' + results[1]);
        }
    }
    else { window.close(); }
};
///<summary>
/// updates the list.
///</summary>
function updateList() {
    chatProg.getRooms(setList);
};
///<summary>
/// sets the list.
///</summary>
///<param name="results" optional="false">the string of results for rooms.</param>
function setList(results) {
    clearlistbox(document.getElementById("lbRoomNames"));
    // Create an Option object        
    // Add an Option object to Drop Down/List Box
    for (var i = 0; i < results.length; i++) {
        var opt = document.createElement("option");
        document.getElementById("lbRoomNames").options.add(opt);
        // Assign text and value to Option object
        opt.text = results[i];
        opt.value = results[i];
        delete opt;
    }
};
///<summary>
/// the user can either create  new group through entering the textbox or select a open group with selecting on a listbox.
///</summary>
///<param name="textboxID" optional="false">textbox ID of the group entered.</param>
///<param name="listBoxID" optional="false">listbox id of the group selected.</param>
function JoinGroup(textBoxID, listBoxID, url) {
    var roomName = document.getElementById(textBoxID).value;
    var listBox = document.getElementById(listBoxID);
    var len = listBox.length;
    for (i = 0; i < len; i++) {
        if (listBox[i].selected) {
            commonPopup(url + '/IM/groupChat.aspx?r=' + listBox[i].text, 500, 350, 4, 'Chat' + listBox[i].text);
        }
    }
    if (roomName != '') {
        chatProg.addToRooms(roomName);
        commonPopup(url + '/IM/groupChat.aspx?r=' + roomName, 500, 350, 4, 'Chat' + roomName);
    }
};




